﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using deFramework; 

namespace deFramework.Base
{
    public class BaseWebService : System.Web.Services.WebService
    {

        /// <summary>
        /// 
        /// </summary>
        public Common.PresentationContext MyContext = null;


        /// <summary>
        /// 
        /// </summary>
        public BaseWebService()
        {
            try 
            { 
                this.MyContext = new Common.PresentationContext(this); 
            }
            catch (Exception ex) 
            {
                this.CurrentException = ex;
                // throw ex; // 
            }
          
        }

  



        #region "Polymorphism - MyLog, CurrentException, StandardLog"

        /// <summary>
        /// 
        /// </summary>
        public Common.FrameworkLogging MyLog
        {
            get { return this.MyContext.MyLog; }
        }

        /// <summary>
        /// 
        /// </summary>
        public System.Exception CurrentException
        {
            get
            {
                Exception _ex = null;
                try
                {
                    _ex = this.MyContext.CurrentException;
                }
                catch (Exception ex)
                {
                    _ex = ex;
                }
                return _ex;
            }
            set
            {
                Exception _ex = value;
                try
                {
                    this.MyContext.CurrentException = _ex;
                }
                catch
                {
                    // do nothing 
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="aMethodName"></param>
        /// <param name="aMessage"></param>
        /// <returns></returns>
        public bool StandardLog(string aMethodName, string aMessage)
        {
            return this.MyContext.StandardLog(aMethodName, aMessage);
        }



        #endregion 


        /// <summary>
        /// 
        /// </summary>
        /// <param name="systemName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        protected bool AuthenticateWebServiceSystem(string serviceName, string systemName, string password)
        {
            //return true;
            bool _result = false;
            try
            {
                string keyService = serviceName + "systems";
                string valueServiceSystems = this.RunModeConfigSetting(keyService, "NO-VALID-SYSTEMS");
                if (valueServiceSystems.ToUpper().Contains(systemName.ToUpper().Trim()))
                {
                    string keySystem = systemName + "passwordDecrypt";
                    //string encryptedValueSystem = this.RunModeConfigSetting(keyService, "NO-VALID-PASSWORD");
                    string encryptedValueSystem = this.Sha1Encryption(keySystem); 
                    _result = (encryptedValueSystem == password);
                }
            }
            catch (Exception ex)
            {
                this.CurrentException = ex;
            }
            return _result;
        }

        #region "Polymorphism - RunModeConfigSetting, ConfigAppSettingValue, Sha1Encryption"

        public string RunModeConfigSetting(string key, string defaultValue)
        {
            return this.MyContext.RunModeConfigSetting(key,defaultValue); 
        }

        public string ConfigAppSettingValue(string fullSettingName, string defaultValue)
        {
            return this.MyContext.ConfigAppSettingValue(fullSettingName,defaultValue); 
        }

        public string Sha1Encryption(string sourceConfigSettingName)
        {
            return this.MyContext.Sha1Encryption(sourceConfigSettingName);
        }

#endregion 

    }
}


#region "Dead Code" 


//string keySystem = systemName + "password";
//string valueSystem = this.RunModeConfigSetting(keySystem, "NO-PASSWORD-REQUIRED");


//string _result = defaultValue;
//try
//{
//    string runMode = this.ConfigAppSettingValue("RunMode", "Dev");
//    _result = System.Configuration.ConfigurationManager.AppSettings[runMode + key].ToString();
//}
//catch (Exception ex)
//{
//    this.CurrentException = ex; 
//}


//string _result = defaultValue;
//try
//{
//    _result = System.Configuration.ConfigurationManager.AppSettings[fullSettingName].ToString();
//}
//catch (Exception ex)
//{
//    this.CurrentException = ex;
//}
            

#endregion 